package tr.edu.yildiz.ce.app_manager.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import tr.edu.yildiz.ce.app_manager.dao.ApplicationDao;
import tr.edu.yildiz.ce.app_manager.model.Application;
import tr.edu.yildiz.ce.app_manager.service.ApplicationService;

@Configurable(preConstruction=true)
public class DownloadServlet extends HttpServlet{

	@Autowired
	ApplicationDao applicationDao;
	
	
	 @Override
	  public void init(ServletConfig config) throws ServletException {
	    super.init(config);
	    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(
	      config.getServletContext());
	    applicationDao = (ApplicationDao) wac.getBean("applicationDaoImpl", ApplicationDao.class);
	 }
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String idStr = req.getParameter("applicationId");
		Long id = Long.parseLong(idStr);
		String xml = null;
		try {
			xml = applicationDao.getApplicationXml(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resp.setContentType("application/xml");
		resp.getWriter().write(xml);
	}
	
	
}
